package com.outsourcing.bottle.ui;

import java.util.HashMap;

import org.json.JSONObject;

import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.BasicUIEvent;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.UICore;
/**
 * 
 * @author 06peng
 *
 */
public class SettingContactActivity extends BasicActivity implements BasicUIEvent, Callback, OnClickListener {

	private ImageView backView;
	private ImageView confirmView;
	private EditText mobileView;
	private EditText qqView;
	private EditText msnView;
	
	private Handler handler;
	private final int set_contact = 1;
	private final int error = 2;
	private final int get_contact_info = 3;
	private String mobile;
	private String msn;
	private String qq;
	
	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.setting_contact);
		handler = new Handler(this);
		initWidget();
		UICore.eventTask(this, this, get_contact_info, "get_contact_info", null);
	}
	
	private void initWidget() {
		backView = (ImageView) findViewById(R.id.topbar_back);
		backView.setOnClickListener(this);
		confirmView = (ImageView) findViewById(R.id.topbar_confirm);
		confirmView.setOnClickListener(this);
		
		mobileView = (EditText) findViewById(R.id.setting_contact_mobile);
		qqView = (EditText) findViewById(R.id.setting_contact_qq);
		qqView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				if (actionId == EditorInfo.IME_ACTION_UNSPECIFIED) {
					UICore.eventTask(SettingContactActivity.this, SettingContactActivity.this, set_contact, "set_contact", null);
				}
				return false;
			}
		});
		msnView = (EditText) findViewById(R.id.setting_contact_msn);
	}
	
	@Override
	public void onClick(View v) {
		if (v == backView) {
			finish();
		} else if (v == confirmView) {
			UICore.eventTask(this, this, set_contact, "set_contact", null);
		}
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case error:
			onToast((String) msg.obj);
			break;
		case set_contact:
			onToast((String) msg.obj);
			finish();
			break;
		case get_contact_info:
			if (mobile != null) {
				mobileView.setText(mobile);
			} 
			if (msn != null) {
				msnView.setText(msn);
			}
			if (qq != null) {
				qqView.setText(qq);
			}
			break;
		default:
			break;
		}
		return false;
	}
	
	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case set_contact:
			setContactInfo();
			break;
		case get_contact_info:
			getContactInfo();
			break;
		default:
			break;
		}
	}
	
	private void setContactInfo() {
		String url = UrlConfig.set_contact_info;
		HashMap<String, String> paramsMap = new HashMap<String, String>();
		paramsMap = new HashMap<String, String>();
		paramsMap.put("login_uid", String.valueOf(AppContext.getInstance().getLogin_uid()));
		paramsMap.put("login_token", AppContext.getInstance().getLogin_token());
		paramsMap.put("mobile", mobileView.getText().toString().trim());
		paramsMap.put("QQ", qqView.getText().toString().trim());
		paramsMap.put("MSN", msnView.getText().toString().trim());
		try {
			String result = HttpUtils.doPost(this, url, paramsMap);
			if (result != null) {
	        	JSONObject object = new JSONObject(result);
	        	JSONObject resultObj = object.getJSONObject("results");
	        	int success = resultObj.getInt("success");
	        	if (success == 0) {
	        		String errmsg = resultObj.getString("errmsg");
	        		Message msg = Message.obtain(handler, error, errmsg);
	        		handler.sendMessage(msg);
	        	} else {
	        		String successmsg = resultObj.getString("successmsg");
					Message msg = Message.obtain(handler, set_contact, successmsg);
					handler.sendMessage(msg);
	        	}
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void getContactInfo() {
		StringBuffer url = new StringBuffer(UrlConfig.get_contact_info);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
	        	JSONObject object = new JSONObject(result);
	        	JSONObject resultObj = object.getJSONObject("results");
	        	int success = resultObj.getInt("authok");
	        	if (success == 0) {
	        		String errmsg = resultObj.getString("errmsg");
	        		Message msg = Message.obtain(handler, error, errmsg);
	        		handler.sendMessage(msg);
	        	} else {
	        		JSONObject contactObj = object.getJSONObject("contact_info");
	        		mobile = contactObj.getString("mobile");
	        		msn = contactObj.getString("MSN");
	        		qq = contactObj.getString("QQ");
					handler.sendEmptyMessage(get_contact_info);
	        	}
	        }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
